package tech.abing.spider.task.pageprocessor;

import java.util.List;
import java.util.regex.Pattern;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import tech.abing.spider.component.PageProcessor;
import tech.abing.spider.component.PageScript;
import tech.abing.spider.model.Proccessable;
import tech.abing.spider.page.OkPage;
import tech.abing.spider.page.Page;
import tech.abing.spider.request.BasicRequest;
import tech.abing.spider.request.StartContext;
import tech.abing.spider.task.model.RateInfo;

public class RateProccessor1 implements PageProcessor {

	static Pattern normalContain = Pattern.compile("天猫");//正常页面必定会存在的字符，你也可以使用页面上其他的字符
	
	@Override
	public PageScript getJavaScript() {
		return null;
	}

	@Override
	public Pattern getNormalContain() {
		return normalContain;
	}

	@Override
	public void process(OkPage page, StartContext context, List<BasicRequest> queue, List<Proccessable> objectContainer)
			throws Exception {
		
		
		String shopId = page.getRequest().getParamsByName("shopId").toString();
		String describe = "";
		String service = "";
		String delivery = "";
		
		String describe_five = "";
		String describe_four = "";
		String describe_three = "";
		String describe_two = "";
		String describe_one = "";
		
		String service_five = "";
		String service_four = "";
		String service_three = "";
		String service_two = "";
		String service_one = "";
		
		String delivery_five = "";
		String delivery_four = "";
		String delivery_three = "";
		String delivery_two = "";
		String delivery_one = "";
		
		 Document doc = Jsoup.parse(page.getContent());
		 Elements eles = doc.select("ul[id=dsr] li");
		 int count = 1;
		 for(Element x:eles){
				if(count ==1){
					describe = x.select("div[class=item-scrib] em[class=count]").attr("title").replace("分", "");
					describe_five = x.select("div[class=box-wrap] div[class=count count5] em[class=h]").text().replace("%", "");
					describe_four = x.select("div[class=box-wrap] div[class=count count4] em[class=h]").text().replace("%", "");
					describe_three = x.select("div[class=box-wrap] div[class=count count3] em[class=h]").text().replace("%", "");
					describe_two = x.select("div[class=box-wrap] div[class=count count2] em[class=h]").text().replace("%", "");
					describe_one = x.select("div[class=box-wrap] div[class=count count1] em[class=h]").text().replace("%", "");
					
				}else if(count == 2){
					service = x.select("div[class=item-scrib] em[class=count]").attr("title").replace("分", "");
					service_five = x.select("div[class=box-wrap] div[class=count count5] em[class=h]").text().replace("%", "");
					service_four = x.select("div[class=box-wrap] div[class=count count4] em[class=h]").text().replace("%", "");
					service_three = x.select("div[class=box-wrap] div[class=count count3] em[class=h]").text().replace("%", "");
					service_two = x.select("div[class=box-wrap] div[class=count count2] em[class=h]").text().replace("%", "");
					service_one = x.select("div[class=box-wrap] div[class=count count1] em[class=h]").text().replace("%", "");
				}else if(count == 3){
					delivery = x.select("div[class=item-scrib] em[class=count]").attr("title").replace("分", "");
					delivery_five = x.select("div[class=box-wrap] div[class=count count5] em[class=h]").text().replace("%", "");
					delivery_four = x.select("div[class=box-wrap] div[class=count count4] em[class=h]").text().replace("%", "");
					delivery_three = x.select("div[class=box-wrap] div[class=count count3] em[class=h]").text().replace("%", "");
					delivery_two = x.select("div[class=box-wrap] div[class=count count2] em[class=h]").text().replace("%", "");
					delivery_one = x.select("div[class=box-wrap] div[class=count count1] em[class=h]").text().replace("%", "");
				}
				count++;
		}
		RateInfo rateInfo = new RateInfo();
		rateInfo.setShopId(shopId);
		rateInfo.setDelivery(delivery);
		rateInfo.setDelivery_five(delivery_five);
		rateInfo.setDelivery_four(delivery_four);
		rateInfo.setDelivery_three(delivery_three);
		rateInfo.setDelivery_two(delivery_two);
		rateInfo.setDelivery_one(delivery_one);
		rateInfo.setDescribe(describe);
		rateInfo.setDescribe_five(describe_five);
		rateInfo.setDescribe_four(describe_four);
		rateInfo.setDescribe_three(describe_three);
		rateInfo.setDescribe_two(describe_two);
		rateInfo.setDescribe_one(describe_one);
		rateInfo.setService(service);
		rateInfo.setService_five(service_five);
		rateInfo.setService_four(service_four);
		rateInfo.setService_three(service_three);
		rateInfo.setService_two(service_two);
		rateInfo.setService_one(service_one);
		objectContainer.add(rateInfo);

	}

	@Override
	public void processErrorPage(Page page, StartContext context,
			List<Proccessable> objectContainer) throws Exception {
		// TODO Auto-generated method stub
		
	}


}
